package com.test3;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListTest {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList<>();

        //添加元素
        Students s1 = new Students("刘德华",20);
        Students s2 = new Students("黎明",22);
        Students s3 = new Students("张学友",18);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        //删除元素
        arrayList.remove(new Students("刘德华",20));
        //重写equals方法之前：3,没有删除成功
        //重写equals方法之后：2,删除成功
        System.out.println(arrayList.size());
        //遍历元素
         //迭代器
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Students s = (Students) it.next();
            System.out.println(s.toString());
        }
         //列表迭代器(从前往后)
        ListIterator lit = arrayList.listIterator();
        while (lit.hasNext()){
            Students s = (Students) lit.next();
            System.out.println(s.toString());
        }
         //列表迭代器(从后往前)
        //如果是直接逆序，这里就为false，但如果是先正序遍历，再进行逆序，这里就为true
        System.out.println(lit.hasPrevious());
        while (lit.hasPrevious()){
            Students ss = (Students) lit.previous();
            System.out.println(ss.toString());
        }
        //判断
        System.out.println(arrayList.contains(new Students("张学友",18)));//true
        System.out.println(arrayList.isEmpty());//false
        //查找
        System.out.println(arrayList.indexOf(s1));//-1
        System.out.println(arrayList.indexOf(s2));//0
    }
}
